package net.bolue.sysmng.db;

import java.util.LinkedList;

import net.bolue.sysmng.util.PageVo;

/**
 * 查询语句类
 * @author Administrator
 *
 */
public class SelectSqlOp {

	private String fields;
	
	private String table;
	
	private String join;
	
	private String where;
	
	private String groupBy;
	
	private String having;
	
	private String orderBy;
	
	private String limit;
	
	private LinkedList<Object> paramterList;
	
	private PageVo pageVo;

	public String getFields() {
		return fields;
	}

	public void setFields(String fields) {
		this.fields = fields;
	}

	public String getTable() {
		return table;
	}

	public void setTable(String table) {
		this.table = table;
	}

	public String getWhere() {
		return where;
	}

	public void setWhere(String where) {
		this.where = where;
	}

	public String getGroupBy() {
		return groupBy;
	}

	public void setGroupBy(String groupBy) {
		this.groupBy = groupBy;
	}

	public String getHaving() {
		return having;
	}

	public void setHaving(String having) {
		this.having = having;
	}

	public String getOrderBy() {
		return orderBy;
	}

	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}

	public String getLimit() {
		return limit;
	}

	public void setLimit(String limit) {
		this.limit = limit;
	}

	public String getJoin() {
		return join;
	}

	public void setJoin(String join) {
		this.join = join;
	}

	public LinkedList<Object> getParamterList() {
		return paramterList;
	}

	public void setParamterList(LinkedList<Object> paramterList) {
		this.paramterList = paramterList;
	}
	
	public PageVo getPageVo() {
		return pageVo;
	}

	public void setPageVo(PageVo pageVo) {
		this.pageVo = pageVo;
		if(this.pageVo!=null){
			this.limit = this.pageVo.getFirstResult()+","+this.pageVo.getMaxResults();
			if(this.pageVo.getSortname()!=null&&!"".equals(this.pageVo.getSortname())){
				this.orderBy = this.pageVo.getSortname();
			}
			if(this.pageVo.getSortorder()!=null&&!"".equals(this.pageVo.getSortorder())){
				this.orderBy = this.orderBy+" "+this.pageVo.getSortorder();
			}
		}
	}

	public Object[] getParamters(){
		if(this.paramterList==null||this.paramterList.size()<1){
			return null;
		}
		Object[] paramters = new Object[this.paramterList.size()];
		for(int i=0;i<this.paramterList.size();i++){
			paramters[i] = this.paramterList.get(i);
		}
		return paramters;
	}

	public String getSelectSql(){
		if(this.fields==null||"".equals(this.fields)||this.table==null||"".equals(this.table)){
			return null;
		}
		String selectSql = "SELECT "+this.fields+" FROM "+this.table+" ";
		if(this.join!=null&&!"".equals(this.join)){
			selectSql = selectSql + " "+this.join;
		}
		if(this.where!=null&&!"".equals(this.where)){
			selectSql = selectSql + " WHERE "+this.where;
		}
		if(this.groupBy!=null&&!"".equals(this.groupBy)){
			selectSql = selectSql + " GROUP BY " + this.groupBy;
		}
		if(this.having!=null&&!"".equals(this.having)){
			selectSql = selectSql + " HAVING " + this.having;
		}
		if(this.orderBy!=null&&!"".equals(this.orderBy)){
			selectSql = selectSql + " ORDER BY " + this.orderBy;
		}
		if(this.limit!=null&&!"".equals(this.limit)){
			selectSql = selectSql + " LIMIT " + this.limit;
		}
		return selectSql;
	}
	
	public String getCountSql(){
		if(this.fields==null||"".equals(this.fields)||this.table==null||"".equals(this.table)){
			return null;
		}
		String countSql = "SELECT COUNT(*) FROM "+this.table+" ";
		if(this.join!=null&&!"".equals(this.join)){
			countSql = countSql + " "+this.join;
		}
		if(this.where!=null&&!"".equals(this.where)){
			countSql = countSql + " WHERE "+this.where;
		}
		if(this.groupBy!=null&&!"".equals(this.groupBy)){
			countSql = countSql + " GROUP BY " + this.groupBy;
		}
		if(this.having!=null&&!"".equals(this.having)){
			countSql = countSql + " HAVING " + this.having;
		}
		return countSql;
	}
	
}
